package com.hatom.cameraxhelp.fragment.face;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.camera.core.ImageProxy;
import androidx.camera.view.PreviewView;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.hatom.cameraxhelp.R;
import com.hatom.cameraxhelp.utils.CameraXHelp;
import com.hatom.cameraxhelp.widgets.MultFaceView;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * @author tangxianfeng
 * 比较简单的调用方式
 * 可用封装类进行直接回调处理
 */
public class SimpleFaceCameraFragment extends Fragment {


    private PreviewView camera_preview;//预览页面

    private MultFaceView multFaceView;//多人脸View

    private CameraXHelp cameraXHelp;//CrameX工具类

    public SimpleFaceCameraFragment() {

    }

    public static SimpleFaceCameraFragment newInstance() {
        SimpleFaceCameraFragment fragment = new SimpleFaceCameraFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camerax_fragment_face_camerax, container, false);
        initView(view);
        return view;
    }


    //布局 监听init
    private void initView(View view) {
        camera_preview = view.findViewById(R.id.simple_camera_preview);
        AppCompatImageView cameraTrans=view.findViewById(R.id.multface_trans_camera);

        multFaceView = view.findViewById(R.id.multFaceView);

        cameraTrans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cameraXHelp!=null){
                    cameraXHelp.transCamera();
                }
            }
        });

        XXPermissions.with(requireActivity())
                .permission(Permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all){
                            initCamera();
                        }else {
                            ToastUtils.showShort("相机权限未通过");
                        }
                    }
                });
    }

    //初始化相机
    private void initCamera(){
        cameraXHelp=new CameraXHelp();//初始化工具类对象
        cameraXHelp.setUpCamera(SimpleFaceCameraFragment.this,requireActivity(), camera_preview.getSurfaceProvider(), new CameraXHelp.AnalaysListener() {
            @Override
            public void analyze(@NonNull @NotNull ImageProxy imageProxy) {
                multFaceView.detect(imageProxy);
            }

            @Override
            public void error(String error) {

            }
        });

        multFaceView.setFaceClickListener(new MultFaceView.FaceClickListener() {
            @Override
            public void clickface(Bitmap bitmap) {

            }

            @Override
            public void clickface(int postion) {
                postion++;
                ToastUtils.showShort("你点了第"+postion+"张图片");
            }
        });
    }
}
